package com.aletto.general_store.utils;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.aletto.general_store.GeneralStoreApplication;
import com.aletto.general_store.R;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 全局混用工具
 */
public class XHelper {

    public static void initTitle(Activity activity, @StringRes int title) {
        activity.findViewById(R.id.back_image).setOnClickListener(v -> activity.finish());
        TextView titleText = activity.findViewById(R.id.title_text);
        titleText.setText(title);
    }

    public static void setVisibility(int visibility, View... views) {
        for (View view : views) {
            view.setVisibility(visibility);
        }
    }

    public static String format(String price) {

        return new DecimalFormat("###.##").format(new BigDecimal(price));
    }

    public static SpannableString toPrice(String price) {

        return toPrice(new BigDecimal(price == null ? "0" : price.trim()));
    }

    /**
     * 转换为价格
     */
    public static SpannableString toPrice(BigDecimal price) {
        String priceString = new DecimalFormat("###.####").format(price);
        SpannableString priceSpannable = new SpannableString(priceString);
        if (priceString.contains(".")) {
            priceSpannable.setSpan(
                    new RelativeSizeSpan(0.7f),
                    priceString.indexOf("."),
                    priceString.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            );
        }

        return priceSpannable;
    }

    /**
     * 复制到剪贴板
     */
    public static void copy2Clipboard(CharSequence content) {
        try {
            ClipboardManager cm = (ClipboardManager) GeneralStoreApplication.sInstance.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData mClipData = ClipData.newPlainText("Label", content != null ? content : "");
            cm.setPrimaryClip(mClipData);
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }

    public static SpannableString matchText(int color, String content, String keyword) {
        SpannableString spannableString = new SpannableString(content);
        keyword = escapeExprSpecialWord(keyword);
        content = escapeExprSpecialWord(content);
        if (content.contains(keyword) && !TextUtils.isEmpty(keyword)) {
            try {
                Pattern pattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(spannableString);
                while (matcher.find()) {
                    spannableString.setSpan(
                            new ForegroundColorSpan(color),
                            matcher.start(),
                            matcher.end(),
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                    );
                }
            } catch (Exception e) {
                return spannableString;
            }
        }

        return spannableString;
    }

    private static String escapeExprSpecialWord(String keyword) {
        if (!TextUtils.isEmpty(keyword)) {
            String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|", "%"};
            for (String key : fbsArr) {
                if (keyword.contains(key)) {
                    keyword = keyword.replace(key, "\\" + key);
                }
            }
        }

        return keyword;
    }

}
